package com.three.storemanage.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.three.core.entity.StoreLabels;
import com.three.core.entity.User;
import com.three.core.vo.R;
import com.three.storemanage.feign.UserFeign;
import com.three.storemanage.mapper.StoreLabelsMapper;
import com.three.storemanage.service.StoreLabelsService;
import com.three.storemanage.vo.LabelsVo;
import com.three.storemanage.vo.SearchForm;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * @FileName: StoreLabelsServiceImpl
 * @Description:
 * @Author: GYX
 * @CreateTime: 2025/3/12 18:22
 * @Version: 1.0.0
 */
@Service
public class StoreLabelsServiceImpl implements StoreLabelsService {
    @Autowired
    private StoreLabelsMapper storeLabelsMapper;

    @Autowired
    private UserFeign userFeign;

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private HttpServletRequest request;
    @Override
    public R<Page<LabelsVo>> findLabelByCondition(SearchForm searchForm) {
        String token = request.getHeader("token");
        Object userName = redisTemplate.opsForHash().get("user::login::" + token, "name");
        R<User> feign = userFeign.getByName((String) userName);
        User user = feign.getData();
        Integer userId = user.getUserId();
        Page<LabelsVo> page = new Page<>(searchForm.getCurrentPage(), searchForm.getPageSize());
        Page<LabelsVo> records = storeLabelsMapper.findLabelByCondition(page,searchForm.getStoreName(),userId);
        return new R<>(200,"查询成功",records);
    }

    @Override
    public R insertLabel(StoreLabels storeLabels) {
        int insert = storeLabelsMapper.insert(storeLabels);
        if (insert > 0){
            return new R<>(200, "添加成功", null);
        }else {
            return new R<>(500, "添加失败", null);
        }
    }

    @Override
    public R deleteLabel(Integer labelId) {
        int i = storeLabelsMapper.deleteById(labelId);
        if (i > 0){
            return new R<>(200, "删除成功", null);
        }else {
            return new R<>(500, "删除失败", null);
        }
    }

    @Override
    public R updateLabel(StoreLabels storeLabels) {
        int i = storeLabelsMapper.updateByPrimaryKeySelective(storeLabels);
        if (i > 0){
            return new R<>(200, "修改成功", null);
        }else {
            return new R<>(500, "修改失败", null);
        }
    }
}
